package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public List<DishItemVO> searchSetmealDish(Long setmealId) {
        return setMealMapper.selectSetmealDishBySetmealId(setmealId);
    }

    @Override
    public List<Setmeal> searchSetmealByCategory(Long categoryId) {
        return setMealMapper.selectSetmealByCategory(categoryId);
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //先插入套餐，通过主键回显获得插入套餐的id
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        int rows=setMealMapper.insertSetmeal(setmeal);
        Long setmealId=setmeal.getId();
        //将套餐id填入setmealDishes列表中
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        for (SetmealDish dish : setmealDishes) {
            dish.setSetmealId(setmealId);
        }
        //将setmealDishes列表中的套餐菜品批量插入setmeal_dish表中
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        setMealMapper.update(Setmeal.builder().id(id).status(status).build());
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改套餐表中的信息
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.update(setmeal);
        //批量删除套餐菜品对应表中该套餐对应的信息
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        List<Long> ids=new ArrayList<>();
        ids.add(setmealDTO.getId());
        setmealDishMapper.deleteBatch(ids);
        //新增DTO中该套餐与菜品的对应关系
        setmealDishMapper.insertBatch(setmealDTO.getSetmealDishes());
    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());//设置分页查询参数，startPage一定要放在方法首行
        //从Mapper层获取所有符合筛选条件的套餐信息
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO,setmeal);
        System.out.println(setmealPageQueryDTO);
        System.out.println(setmeal);
        List<Setmeal> setmealList=setMealMapper.page(setmeal);
        //通过分页插件封装分页查询结果
        PageInfo<Setmeal> pageInfo=new PageInfo<>(setmealList);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public SetmealVO searchSetmealById(Long id) {
        return setMealMapper.selectSetmealById(id);
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //查询可以删除的套餐（套餐处于停售状态）的id有哪些
        List<Long> idsForDel=setMealMapper.selectSetmealForDel(ids);
        if(idsForDel!=null&&!idsForDel.isEmpty()) {
            //删除套餐表中idsForDel对应的套餐
            setMealMapper.deleteBatch(idsForDel);
            //删除套餐菜品对应表中idsForDel对应的套餐菜品对应关系
            setmealDishMapper.deleteBatch(idsForDel);
        }
    }
}
